package net.raebiger.spreadsheetcat.google;

import net.raebiger.spreadsheetcat.R;
import net.raebiger.spreadsheetcat.preference.UserSettingActivity;
import net.raebiger.spreadsheetcat.util.DialogUtil;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.OperationCanceledException;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class AuthenticateAccountAction implements OnClickListener {

	private final UserSettingActivity userSettingActivity;

	private final String AUTH_TOKEN_TYPE = "oauth2:https://spreadsheets.google.com/feeds";

	public AuthenticateAccountAction(UserSettingActivity userSettingActivity) {
		this.userSettingActivity = userSettingActivity;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Log.d("SSC", "Authenticate Accounts");

		SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(userSettingActivity);
		final String userName = preferences.getString("prefUsername", null);
		Account account = userSettingActivity.getStoredGoogleAccountOrNull();
		// AccountManager accountManager =
		// googleAccountManager.getAccountManager();
		AccountManager accountManager = AccountManager.get(userSettingActivity);

		accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, Bundle.EMPTY, userSettingActivity,
				new AccountManagerCallback<Bundle>() {
					private String token = null;

					@Override
					public void run(AccountManagerFuture<Bundle> future) {
						try {
							// If the user has authorized your application to
							// use the tasks API
							// a token is available.
							token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
							// Now you can use the Tasks API...
						} catch (OperationCanceledException e) {
							Log.e("SSC", "Error while authenticating", e);
							DialogUtil.getInstance().showDialog(R.string.error_msg_userDidNotAuthorize,
									userSettingActivity);
						} catch (Exception e) {
							Log.e("SSC", "Error while authenticating", e);
							DialogUtil.getInstance().showDialog(R.string.error_msg_authenticationFailure,
									userSettingActivity);
						}
						if (token != null) {
							showDialogAuthenticationSuccessful(userName);
						}
					}

					private void showDialogAuthenticationSuccessful(final String userName) {
						AlertDialog.Builder builder = new AlertDialog.Builder(userSettingActivity);
						builder.setMessage(String.format(
								userSettingActivity.getString(R.string.auth_msg_authorizationOK), userName));
						builder.setCancelable(false);
						builder.setPositiveButton(userSettingActivity.getString(R.string.OK),
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog, int which) {
										dialog.dismiss();
									}
								});
						builder.create().show();
					}

				}, null);
	}
}
